package kuzmenkov.oip.common;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import java.io.File;
import java.util.logging.Logger;

/**
 * User: Michael Kuzmenkov
 * Date: 18.04.2009
 * Time: 21:36:49
 */
public class FileTool {

    private Logger log = Logger.getLogger(FileTool.class.getName());
    private String workDir;
    private String commonTempDir;
    private static final String WORK_DIR_PARAM = "workDir";
    private static final String COMMON_TEMP_DIR = "temp";
    protected static FileTool instance; 

    protected FileTool () {
        initWorkDir();
        initCommonTempDir();
    }

    public static FileTool getInstance() {
        if (instance==null)
            instance = new FileTool();
        return instance;
    }


    protected void initCommonTempDir() {
        commonTempDir = workDir+ COMMON_TEMP_DIR + File.separator;

        File dir = new File(commonTempDir);
        if (!dir.exists())
            dir.mkdirs();    

    }

    protected String  loadWorkDirParam () {
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext extContext = context.getExternalContext();
        return extContext.getInitParameter(WORK_DIR_PARAM);
    }

    protected void initWorkDir() {
        workDir = loadWorkDirParam();
        
        if (!workDir.endsWith(File.separator)){
             workDir+=File.separator;
        }

        if (!(new File(workDir)).exists())
            throw new IllegalStateException("The path "+workDir+" to " +
                    "the work directory is not exist");

        log.info ("workDir="+workDir);
    }

    /**
     * A path ends with a file separator.
     * @return the path to the work dir e.g. to %catalina_home%\webapps\oip
     */
    public String getWorkDir () {
        return workDir;  
    }

    /**
     * A path ends with a file sepatator.
     * @return the path to the common temp dir
     */
    public String getCommonTempDir () {
        return commonTempDir;
    }

    /**
     * Returns a relative path to a temp dir. The path ends with '/'. 
     * @param sessionId an id of a current session
     * @return the path
     */
    public String getRelativeTempDir (String sessionId) {
        if (sessionId==null || sessionId.isEmpty()){
            throw new IllegalArgumentException("The parameter sessionId is empty");
        }
        return COMMON_TEMP_DIR + "/" + sessionId + "/";
    }


    /**
     * Returns a absolute path to a temp directory. The path ends with a file
     * separator. 
     * @param sessionId an id of a current session
     * @return the path
     */
    public String getTempDir (String sessionId) {
        if (sessionId==null || sessionId.isEmpty()){
            throw new IllegalArgumentException("The parameter sessionId is empty");
        }
        String tempDir = getCommonTempDir()+sessionId+File.separator;
        File dir = new File(tempDir);
        if (!dir.exists())
            dir.mkdirs();
        dir.deleteOnExit();
        return tempDir;
        
    }

    /**
     * Converts a path from relative to absolute
     * @param relPath the relative path
     * @return the absolute path
     */
    public String relPathToAbs (String relPath) {
        if (relPath==null || relPath.isEmpty())
            throw new IllegalArgumentException("relPath is empty: "+relPath+".");
        if (relPath.startsWith("/"))
            relPath = relPath.substring(1,relPath.length());
        String absPath = relPath.replace("/",File.separator);
        FileTool ftool = FileTool.getInstance();
        absPath = ftool.getWorkDir() + absPath;
        return absPath;
    }
}
